<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta content="always" name="referrer">
<meta name="format-detection" content="telephone=no" />
<meta name="viewport"
	content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>首页</title>
<script type="text/javascript" src="/LianniuWeb/tea/js/jquery-1.11.3.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript" src="/LianniuWeb/js/commons/commons.js"></script>
</head>

<body>
	<script type="text/javascript">
		var code = GetRequest().code;
		$(function(){
			$.ajax({
				type : "get",
				url : server_url + 'rest/tea/pay/getOpenId.do?ran='
						+ Math.random(),
				dataType : 'json',
				async : true,
				data : {
					token : token,
					code : code
				},
				success : function(result) {
					var data = JSON.parse(result.data);
					console.log(data);
					var openid = data.openid;
					var accessToken = data.access_token;
					$.ajax({
						type : "get",
						url : server_url + 'rest/tea/base/getUserMsg.do?ran='
								+ Math.random(),
						dataType : 'json',
						async : true,
						data : {
							token : token,
							openid : openid,
							accessToken :accessToken
						},
						success : function(result) {
							console.log("获取用户信息");
							console.log(result);
							var userInfo = JSON.parse(result.data);
							console.log(userInfo);
							localStorage.setItem("userInfo",userInfo);
							localStorage.setItem("openid",userInfo.openid);
							
							
							$.ajax({
								type : "post",
								url : server_url + 'tea/rest/user/registerWeixin.do?ran='
										+ Math.random(),
								dataType : 'json',
								async : true,
								data : {
									token : token,
									openid : openid,
									username : userInfo.nickname,
									picpath : userInfo.headimgurl
								},
								success : function(result) {
									var user = result.data;
									localStorage.setItem("username",user.usersname);
									localStorage.setItem(localStorage.getItem("username"),"1");
									console.log(result);
									localStorage.setItem("userid",user.usersid);
									window.location.href=server_url+"buyer/product/index.do";
								}
							});
							
						}
					});
				}
			});
		});
		function GetRequest() {
			var url = location.search; //获取url中"?"符后的字串
			var theRequest = new Object();
			if (url.indexOf("?") != -1) {
				var str = url.substr(1);
				strs = str.split("&");
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = unescape(strs[i]
							.split("=")[1]);
				}
			}
			return theRequest;
		}
	</script>
</body>

</html>